package com.wm.blog_web.controller.base;

import com.wm.blog_common.constatnt.CommonConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;

/***
 * @ClassName: WebBaseController
 * @Description: admin后台controller基类
 * @Author: wm_yu
 * @Create_time: 22:46 2020-3-24
 */
@Controller
public class WebBaseController {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 当前登录人的id
     * @param request
     * @return
     */
    public Long currentUserId(HttpServletRequest request){
        String token = request.getHeader(CommonConstant.WEB_TOKEN);
        if(StringUtils.isNotEmpty(token)){
            String userId = redisTemplate.opsForValue().get(CommonConstant.WEB_REDIS_PREFIX + token);
            return StringUtils.isEmpty(userId) ? -1:Long.parseLong(userId);
        }
        return -1L;
    }
}
